﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Tb.App.API.Applications.Command.Slaughter;
using Tb.App.Domain.Rabbit.Slaughter;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Slaughter
{
    /// <summary>
    /// 屠宰企业信息查询-命令处理
    /// </summary>
    public class GetSlaughterCommpanyQueryCommandHandler : IRequestHandler<GetSlaughterCommpanyQueryCommand, ApiResult<SlaughterCommpany>>
    {
        private readonly IBaseRepository<SlaughterCommpany> slaughterCommanyRep;

        public GetSlaughterCommpanyQueryCommandHandler(IBaseRepository<SlaughterCommpany> slaughterCommanyRep)
        {
            this.slaughterCommanyRep = slaughterCommanyRep;
        }
        public async Task<ApiResult<SlaughterCommpany>> Handle(GetSlaughterCommpanyQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<SlaughterCommpany> result=new ApiResult<SlaughterCommpany>();
            var entity =await slaughterCommanyRep.GetAll().Where(x => x.Batch == request.Batch && !x.IsDel).OrderByDescending(x=>x.Id).FirstOrDefaultAsync();
            if(entity != null)
            {
                result.Code = ApiEnum.查询成功;
                result.Message = ApiEnum.查询成功.ToString();
                result.Data = entity;
            }
            else
            {
                result.Code = ApiEnum.查询失败;
                result.Message = ApiEnum.查询失败.ToString();
                result.Data = null;
            }

            return result;
        }
    }
}
